package edu.cque.jt.provider.controller.inteceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;
@Slf4j
public class TimeAccessInterceptor  implements HandlerInterceptor {
    /**
     * preHandle在控制层目标方法执行之前执行
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //testRequestInfo(request,handler);
        LocalTime now=LocalTime.now();//JDK8中的时间对象
        int hour=now.getHour();//获取当前时间对应小时
        //System.out.println("hour="+hour);
        log.info("hour {}",hour);
        if(hour<=6||hour>=22)
            throw new RuntimeException("请在6~10点进行访问");
        return true;
    }
}
